clc
close all
clear all


load LIPS_Visual_Params.mat
load all_phoneme_labels_post.mat


%% find all unique phonemes
phonemes = {};
all_phoneme_labels_post_raw = all_phoneme_labels_post;
for i = 1:numel(all_phoneme_labels_post)
    for j = 1:numel(all_phoneme_labels_post{i})
        if all_phoneme_labels_post{i}{j}(end)=='X'
            all_phoneme_labels_post{i}{j}(end) = [];
        end
    end
    phonemes = unique([phonemes all_phoneme_labels_post{i}{:}]);
end
phonemes


%% find occurences of each phonemes
phoneme_ninstances = zeros(1, numel(phonemes));
phoneme_instances = cell(1, numel(phonemes));
for p = 1:numel(phonemes)
    phoneme_instances{p} = {};
    for i = 1:numel(all_phoneme_labels_post)
        frames = find(strcmp(phonemes{p}, all_phoneme_labels_post{i}));
        if numel(frames)
            phoneme_instances{p}{end+1}.seqind = i;
            phoneme_instances{p}{end  }.frames = frames;
            phoneme_instances{p}{end  }.params = LIPS_Visual_Params.all_aam_params{i}(frames, :);
        end
    end
    phoneme_ninstances(p) = numel(phoneme_instances{p});
    
    if abs(diff(frames))>1
        error('Same phoneme found more than once in a sequence')
    end
end


% % % %% compute sample means and variances
% % % ndims = size(LIPS_Visual_Params.all_aam_params{1}, 2);
% % % phoneme_sample_params = cell(1, numel(phonemes));
% % % phoneme_sample_nparams = zeros(1, numel(phonemes));
% % % phoneme_sample_means = zeros(numel(phonemes), ndims);
% % % phoneme_sample_vars = zeros(numel(phonemes), ndims);
% % % for p = 1:numel(phonemes)
% % %     % select the middle frames from 50% of instances
% % %     tempinds = randperm(phoneme_ninstances(p));
% % %     tempinds = tempinds(1:ceil(numel(tempinds)/2));
% % %     
% % %     phoneme_sample_nparams(p) = numel(tempinds);
% % %     
% % %     phoneme_sample_params{p} = [];
% % %     for i = tempinds
% % %         frames = phoneme_instances{p}{i}.frames;
% % %         phoneme_sample_params{p}(end+1, :) = phoneme_instances{p}{i}.params(ceil(numel(frames)/2), :);
% % %     end
% % %     
% % %     phoneme_sample_means(p, :) = mean(phoneme_sample_params{p}, 1);
% % %     phoneme_sample_vars(p, :)  =  var(phoneme_sample_params{p}, [], 1);
% % % end
% % % % % % phoneme_sample_means
% % % % % % phoneme_sample_vars
% % % 
% % % 
% % % %% compute random means and variances
% % % phoneme_all_params = cell(1, numel(phonemes));
% % % phoneme_random_params = cell(1, numel(phonemes));
% % % phoneme_random_means = zeros(numel(phonemes), ndims);
% % % phoneme_random_vars = zeros(numel(phonemes), ndims);
% % % for p = 1:numel(phonemes)
% % %     % select random data
% % %     phoneme_all_params{p} = [];
% % %     for i = 1:numel(phoneme_instances{p})
% % %         phoneme_all_params{p} = [phoneme_all_params{p}; phoneme_instances{p}{i}.params];
% % %     end
% % %     
% % %     tempinds = randperm(size(phoneme_all_params{p}, 1));
% % %     tempinds = tempinds(1:size(phoneme_sample_params{p}, 1));
% % %     phoneme_random_params{p} = phoneme_all_params{p}(tempinds, :);
% % % 
% % %     phoneme_random_means(p, :) = mean(phoneme_random_params{p}, 1);
% % %     phoneme_random_vars(p, :)  =  var(phoneme_random_params{p}, [], 1);
% % % end
% % % % % % phoneme_random_means
% % % % % % phoneme_random_vars
% % % 
% % % 
% % % %% compute speech correlation measure
% % % Dvars_all = (phoneme_random_vars-phoneme_sample_vars)./(phoneme_random_vars+realmin);
% % % 
% % % % should be weighted by the number of samples in phoneme?
% % % figure
% % % plot(sum(Dvars_all))
% % % 
% % % % should be weighted by the number of samples in phoneme?
% % % figure
% % % plot(phoneme_sample_nparams*Dvars_all)


%%
res = computeMutualInformation(phonemes, phoneme_instances);
res.mutual_infos

[pose_infos pose_params] = sort(res.mutual_infos);

figure
plot(res.mutual_infos, '.-')


save results.mat


%% visualise modes of variation

% Save library paths
MatlabPath = getenv('LD_LIBRARY_PATH');
% Make Matlab use system libraries
setenv('LD_LIBRARY_PATH',getenv('PATH'))


% visualise modes of variations
PCAinfo = LIPS_Visual_Params.PCA;
phonemes_new_data = cell(1, numel(res.mean_ica_data));
for d = 1:numel(res.mean_ica_data)
    for c = -2:2
        base_params = res.mean_ica_data;
        base_params(d) = base_params(d) + sqrt(res.vars_ica_data(d))*c;
        phonemes_new_data{d}(end+1, :) = PCAinfo.eigvec*(res.A*base_params) + PCAinfo.mean_vec(:);
    end
    
    fWriteAAMDataFile(phonemes_new_data{d}, 'bah.txt', './')
    system('OpenCV_AAM/aamc g2 lips08_aam_model bah.txt temp/');
    system('./encode.sh');
    system(['mv output.avi output.' sprintf('%2.2d', d) '.avi']);
end


% write original sequence
seq_no = 205;
old_seq = LIPS_Visual_Params.all_concat_params{seq_no};
fWriteAAMDataFile(old_seq, 'bah.txt', './')
system('OpenCV_AAM/aamc g2 lips08_aam_model bah.txt frames/');
system('./encode2.sh');
system(['mv output.avi output.old.avi']);

% write normalised sequence
new_seq = LIPS_Visual_Params.all_concat_params{seq_no};
for f = 1:size(new_seq, 1)
    temp_frame = res.W*(PCAinfo.eigvec'*(new_seq(f, :)-PCAinfo.mean_vec)');
    temp_frame(pose_params(1:3)) = 0;
    new_seq(f, :) = PCAinfo.eigvec*(res.A*temp_frame) + PCAinfo.mean_vec';
end
fWriteAAMDataFile(new_seq, 'bah.txt', './')
system('OpenCV_AAM/aamc g2 lips08_aam_model bah.txt frames/');
system('./encode2.sh');
system(['mv output.avi output.new.avi']);


% Reassign old library paths
setenv('LD_LIBRARY_PATH',MatlabPath)
